import 'package:casdon/core/network/api_client_provider.dart';
import 'package:casdon/core/network/i_api_client.dart';
import 'package:casdon/modules/demo/data/api/demo_ocr_api.dart';
import 'package:casdon/modules/demo/data/repository/demo_repository.dart';

import 'package:casdon/modules/me/data/api/oss_sts_api.dart';

import 'package:flutter_riverpod/flutter_riverpod.dart';

class DemoRepositoryImpl extends DemoRepository {
  // 字段类型也是抽象接口
  final IApiClient _apiClient;

  DemoRepositoryImpl({required IApiClient apiClient}) : _apiClient = apiClient;

  @override
  Future<ResponseOssStsApi?> getOSSStsToken(String userId) async {
    final res = await _apiClient.getTyped<ResponseOssStsApi?>(
      '/client-product/oss/getStsToken',
      query: {'fileType': "image", "type": "cloud-app", 'userId': userId},
      fromJsonT: (json) =>
          ResponseOssStsApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<void> recipeStreamCall(String msgUrl, String text) async {
    final res = await _apiClient.post<void>(
      '/client-product/ai/coffeBeanPackagingStreamCallBatch',
      data: {
        'imageUrls': [msgUrl],
        "text": text,
      },
    );
    return;
  }

  //食谱识别-JSON格式-异步获取任务id-不需要登录
  @override
  Future<String> postRecipeOcr(String msgUrl) async {
    final res = await _apiClient.post<Map<String, dynamic>>(
      '/client-product/ai/recipeJsonCall',
      data: {
        'imageUrls': [msgUrl],
      },
    );
    return res?['msg']?.toString() ?? "";
  }

  @override
  Future<ResponseDemoOcrApi?> getRecipeOcrRsult(String taslId) async {
    final res = await _apiClient.getTyped<ResponseDemoOcrApi>(
      '/client-product/ai/getRecipeJsonCallResult',
      query: {"taskId": taslId},
      fromJsonT: (json) =>
          ResponseDemoOcrApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<void> getAIRecipe(String recipeName) async {
    final res = await _apiClient.get<void>(
      '/client-product/ai/genRecipeInfo',
      query: {"recipeName": recipeName},
    );
    return;
  }
}

final demoRepositoryProvider = Provider<DemoRepository>(
  (ref) => DemoRepositoryImpl(apiClient: ref.read(apiClientProvider)),
);
